package com.ruoyi.project.system.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysAttr;
import com.ruoyi.project.system.domain.SysFood;
import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysAttrService;
import com.ruoyi.project.system.service.ISysFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 属性操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/food")
public class SysFoodController extends BaseController {
    @Autowired
    private ISysFoodService foodService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取食品列表
     */
    @PreAuthorize("@ss.hasPermi('system:food:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysFood food)
    {
        startPage();
        List<SysFood> list = foodService.selectFoodList(food);
        return getDataTable(list);
    }


    /**
     * 获取食品类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:food:list')")
    @GetMapping("/typeList")
    public AjaxResult typeList(SysFood food)
    {
        List<SysType> list = foodService.selectTypeList(food);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:food:add')")
    @Log(title = "商品分类管理", businessType = BusinessType.INSERT)
    @PostMapping("/type")
    public AjaxResult addType(@RequestBody SysFood food)
    {
        SysType sysType=new SysType();
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        if(null!=food){
            sysType.setDeptId(currentUser.getDeptId());
            sysType.setDescription(food.getTypeDescription());
            sysType.setRemark(food.getRemark());
            sysType.setName(food.getTypeName());
            sysType.setCreateBy(SecurityUtils.getUsername());
        }
        foodService.insertType(sysType);
        return AjaxResult.success(foodService.selectTypeList(new SysFood()));
    }



    @PreAuthorize("@ss.hasPermi('system:food:query')")
    @GetMapping(value = "/{foodId}")
    public AjaxResult getInfo(@PathVariable Long foodId)
    {
        return AjaxResult.success(foodService.selectFoodById(foodId));
    }

    @PreAuthorize("@ss.hasPermi('system:food:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addFood(@RequestBody SysFood food)
    {
        food.setCreateBy(SecurityUtils.getUsername());
        return toAjax(foodService.insertFood(food));
    }

    @PreAuthorize("@ss.hasPermi('system:food:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysFood food)
    {
        food.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(foodService.updateFood(food));
    }
   /* @PreAuthorize("@ss.hasPermi('system:attr:remove')")
    @Log(title = "属性管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attrId}")
    public AjaxResult remove(@PathVariable Long attrId)
    {
        return toAjax(attrService.deleteAttrById(attrId));
    }*/
}
